import { mockRequestClient as defHttp } from '#/api/request';
import type { ResourceCapability, ResourceCapabilities } from './types';

enum Api {
  // 能力绑定
  Bind = '/resource/capability/bind',

  // 能力解绑
  Unbind = '/resource/capability/unbind',

  // 能力配置
  Config = '/resource/capability/config',

  // 能力启用/禁用
  Toggle = '/resource/capability/toggle',

  // 能力依赖检查
  CheckDependencies = '/resource/capability/dependencies/check',

  // 能力冲突检查
  CheckConflicts = '/resource/capability/conflicts/check',

  // 能力模板
  Templates = '/resource/capability/templates',

  // 能力市场
  Marketplace = '/resource/capability/marketplace',

  // 能力使用统计
  Usage = '/resource/capability/usage',

  // 能力性能监控
  Performance = '/resource/capability/performance',
}

// 能力绑定参数
export interface BindCapabilityParam {
  resourceId: string;
  capabilityId: string;
  config?: any;
  priority?: number;
  enabled?: boolean;
}

// 能力配置参数
export interface ConfigCapabilityParam {
  resourceId: string;
  capabilityId: string;
  config: any;
}

// 能力模板
export interface CapabilityTemplate {
  id: string;
  name: string;
  description: string;
  type: string;
  category: string;
  version: string;
  config: any;
  dependencies: string[];
  tags: string[];
  icon?: string;
}

// 能力市场项
export interface CapabilityMarketItem {
  id: string;
  name: string;
  description: string;
  type: string;
  category: string;
  version: string;
  price: {
    currency: string;
    amount: number;
    type: 'free' | 'paid' | 'subscription';
  };
  rating: number;
  downloads: number;
  author: string;
  tags: string[];
  icon?: string;
}

// 能力使用统计
export interface CapabilityUsageStats {
  capabilityId: string;
  resourceId: string;
  usageCount: number;
  lastUsed: string;
  performance: {
    avgResponseTime: number;
    successRate: number;
    errorRate: number;
  };
}

/**
 * 资源能力挂载API
 */
export const ResourceCapabilityApi = {
  /**
   * 绑定能力到资源
   */
  bind: (params: BindCapabilityParam) =>
    defHttp.post<{ success: boolean; message: string }>({
      url: Api.Bind,
      data: params,
    }),

  /**
   * 从资源解绑能力
   */
  unbind: (resourceId: string, capabilityId: string) =>
    defHttp.delete<{ success: boolean; message: string }>({
      url: `${Api.Unbind}/${resourceId}/${capabilityId}`,
    }),

  /**
   * 配置能力
   */
  config: (params: ConfigCapabilityParam) =>
    defHttp.put<{ success: boolean; message: string }>({
      url: Api.Config,
      data: params,
    }),

  /**
   * 启用/禁用能力
   */
  toggle: (resourceId: string, capabilityId: string, enabled: boolean) =>
    defHttp.put<{ success: boolean; message: string }>({
      url: `${Api.Toggle}/${resourceId}/${capabilityId}`,
      data: { enabled },
    }),

  /**
   * 检查能力依赖
   */
  checkDependencies: (resourceId: string, capabilityId: string) =>
    defHttp.get<{
      satisfied: boolean;
      missing: string[];
      conflicts: string[];
    }>({
      url: `${Api.CheckDependencies}/${resourceId}/${capabilityId}`,
    }),

  /**
   * 检查能力冲突
   */
  checkConflicts: (resourceId: string, capabilityId: string) =>
    defHttp.get<{
      hasConflicts: boolean;
      conflicts: Array<{
        type: 'version' | 'config' | 'dependency';
        description: string;
        severity: 'low' | 'medium' | 'high';
      }>;
    }>({
      url: `${Api.CheckConflicts}/${resourceId}/${capabilityId}`,
    }),

  /**
   * 获取能力模板列表
   */
  getTemplates: (params?: {
    type?: string;
    category?: string;
    tags?: string[];
  }) =>
    defHttp.get<CapabilityTemplate[]>({
      url: Api.Templates,
      params,
    }),

  /**
   * 获取能力市场列表
   */
  getMarketplace: (params?: {
    type?: string;
    category?: string;
    tags?: string[];
    priceType?: 'free' | 'paid' | 'subscription';
    sortBy?: 'rating' | 'downloads' | 'price' | 'date';
    sortOrder?: 'asc' | 'desc';
  }) =>
    defHttp.get<{
      items: CapabilityMarketItem[];
      total: number;
      facets: {
        types: Record<string, number>;
        categories: Record<string, number>;
        priceTypes: Record<string, number>;
        tags: Record<string, number>;
      };
    }>({
      url: Api.Marketplace,
      params,
    }),

  /**
   * 获取能力使用统计
   */
  getUsageStats: (resourceId: string, capabilityId?: string) =>
    defHttp.get<CapabilityUsageStats[]>({
      url: `${Api.Usage}/${resourceId}`,
      params: { capabilityId },
    }),

  /**
   * 获取能力性能监控
   */
  getPerformance: (
    resourceId: string,
    capabilityId: string,
    timeRange: string,
  ) =>
    defHttp.get<{
      metrics: Array<{
        timestamp: string;
        responseTime: number;
        successRate: number;
        errorRate: number;
        throughput: number;
      }>;
      summary: {
        avgResponseTime: number;
        avgSuccessRate: number;
        avgErrorRate: number;
        totalRequests: number;
      };
    }>({
      url: `${Api.Performance}/${resourceId}/${capabilityId}`,
      params: { timeRange },
    }),
};
